/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.CFields;
import DTO.CKho;
import DTO.CSanPham;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class CKhoDAO extends IData<CKho>{
    public CKhoDAO(){
        
    }

    @Override
    public CKho add(CKho item) {
        try {
            ps.setString(1, item.getDiaChi());
            ps.setBoolean(2, item.getCuaHang());
            ps.setString(3, item.getDienThoai());
            ps.setString(4, item.getTenKho());
            ps.setString(5, item.getFax());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
       
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CKho> list, SubConfig config) {
        String mysql ="insert into " + CFields.tb_kho 
                + "(" + CFields.dia_chi + "," + CFields.cua_hang +","
                + CFields.dien_thoai + "," + CFields.ten_kho + ","
                + CFields.fax
                + ") values(?,?,?,?,?)";
        int rs = 0;
        try {
            ps = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
             for(CKho item : list){
                this.add(item);
            }
           int []n = ps.executeBatch();
           rs = n.length;
        } catch (SQLException ex) {
            Logger.getLogger(CKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    @Override
    public CKho Find(CKho item, List<CKho> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(CKho item, int t, SubConfig config) {
         String mysql ="insert into " + CFields.tb_kho 
                + "(" + CFields.dia_chi + "," + CFields.cua_hang +","
                + CFields.dien_thoai + "," + CFields.ten_kho + ","
                + CFields.fax
                + ") values(?,?,?,?,?)";
         Object result = 0;
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setString(1, item.getDiaChi());
            cs.setBoolean(2, item.getCuaHang());
            cs.setString(3, item.getDienThoai());
            cs.setString(4, item.getTenKho());
            cs.setString(5, item.getFax());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {                
                result = (long) keyResultSet.getInt(CFields.id);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
          return result;
    }

    @Override
    public Object insertOnSubmit(CKho item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(CKho item, SubConfig config) {
        Object result = 0;
        String mysql = "update " + CFields.tb_kho + " set "
                + CFields.dia_chi + "=?," + CFields.dien_thoai + "=?,"
                + CFields.fax + "=?," + CFields.cua_hang +"=?,"
                + CFields.ten_kho + "=? where " + CFields.id + "=?";                
    
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setString(1, item.getDiaChi());
            cs.setString(2, item.getDienThoai());
            cs.setString(3, item.getFax());
            cs.setBoolean(4, item.getCuaHang());
            cs.setString(5, item.getTenKho());
            cs.setLong(6, item.getId());
            cs.executeUpdate();/*
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);      
            }*/
        } catch (SQLException ex) {
            Logger.getLogger(CKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
       
        return result;
    }

    @Override
    public Object deleteOnSubmint(CKho item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CKho getItem(ResultSet rs) {
        CKho item = new CKho();
        try {
            item.setId(rs.getLong(CFields.id));
            item.setTenKho(rs.getString(CFields.ten_kho));
            item.setFax(rs.getString(CFields.fax));
            item.setDiaChi(rs.getString(CFields.dia_chi));
            item.setDienThoai(rs.getString(CFields.dien_thoai));
            item.setCuaHang(rs.getBoolean(CFields.cua_hang));
        } catch (SQLException ex) {
            Logger.getLogger(CKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<CKho> getAll(SubConfig config) {
        //this.sWhere = " where delete = false ";
        String mysql = "select * from vKho " + " " + this.sWhere;
        return this.getAll(mysql, config);
    }

    @Override
    protected void add(CKho item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

  
}
